package route

import (
	"fmt"
	"svvenus/vgame/logic"
	"svvenus/vpub/constants"
	"svvenus/zlog"
	"time"
)

// SchedulePerMinutePlayers 分钟事件
func SchedulePerMinutePlayers() {
	zlog.Debug("SchedulePerMinutePlayer")

	db := constants.MgoMain.New()
	defer db.Close()

	//断线玩家清除出内存
	listDelete := make([]string, 0)
	logic.MapPlayer.Range(func(k, v interface{}) bool {
		player := v.(*logic.Player)
		if player.Online {
			//玩家分钟心跳
			player.SchedulePerMinute(db)
		} else {
			//判断断线
			if constants.SvTime.After(player.TimeDisconnection.Add(time.Minute * 10)) {
				player.Save(db, false)
				listDelete = append(listDelete, player.IdString())
			}
		}
		return true
	})
	for _, v := range listDelete {
		zlog.Debug(fmt.Sprintf("玩家 %s 清除出内存", v))
		logic.MapPlayer.Delete(v)
	}

	//检测是否有新的全服邮件
	logic.MapPlayer.Range(func(k, v interface{}) bool {
		player := v.(*logic.Player)
		if player.GlobalMailVersion < logic.ServerData.GlobalMailVersion {
			for _, doc := range logic.GlobalMails {
				if player.GlobalMailVersion < doc.Version {
					player.GlobalMailAddToPlayer(doc)
				}
			}
			player.GlobalMailVersion = logic.ServerData.GlobalMailVersion
		}
		return true
	})
}

// ScheduleOClock0Players
// @Description: 玩家0点更新
func ScheduleOClock0Players() {
	zlog.Debug("ScheduleOClock0Players")

	logic.MapPlayer.Range(func(k, v interface{}) bool {
		player := v.(*logic.Player)
		player.ScheduleOClock0()
		return true
	})
}

// ScheduleOClock5Players
// @Description: 玩家5点更新
func ScheduleOClock5Players() {
	zlog.Debug("ScheduleOClock5Players")

	logic.MapPlayer.Range(func(k, v interface{}) bool {
		player := v.(*logic.Player)
		player.ScheduleOClock5()
		return true
	})
}
